<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="盒子分类名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入盒子分类名称"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-input
          v-model="queryParams.time"
          placeholder="请输入时间"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入${comment}"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="createDate">
        <el-date-picker
          clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fl:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:fl:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fl:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:fl:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getFlList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="flList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'twos', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" sortable width="180">
      </el-table-column>
      <el-table-column prop="title" label="名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fl:edit']"
            >修改</el-button
          >
          <!-- 只有父级显示添加 -->
          <el-button
            type="success"
            size="mini"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:fl:add']"
            v-if="scope.row.twos"
            >添加</el-button
          >

          <!-- 查看关联 -->
          <!-- <el-button
            type="info"
            size="mini"
            @click="handlelookRelation(scope.row)"
            v-hasPermi="['system:fl:view']"
            v-if="!scope.row.twos"
            >查看关联</el-button
          > -->

          <!-- 产生关联 -->
          <el-button
            type="warning"
            size="mini"
            @click="handleAddRelation(scope.row)"
            v-hasPermi="['system:fl:view']"
            v-if="!scope.row.twos"
            >产生关联</el-button
          >
          <!-- 查看关联 -->
          <el-button
            type="info"
            size="mini"
            @click="handlelookRelation(scope.row)"
            v-hasPermi="['system:fl:view']"
            v-if="!scope.row.twos"
            >查看关联</el-button
          >

          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:fl:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改盒子分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="一级分类名称" prop="title">
          {{ this.title }}
        </el-form-item>

        <el-form-item label="分类名称" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入名称"
            clearable
            
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 快捷导入 -->
    <el-dialog
      title="快捷导入"
      :visible.sync="dialogVisible_1"
      width="80%"
      center
    >
      <importForm @func="update" />
    </el-dialog>

    <el-dialog title="查看关联" :visible.sync="dialogTableVisible_2">
      <el-table :data="gridData">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="盒子ID" align="center" prop="id" />

        <el-table-column label="商品名称" align="center" prop="title" />
        <el-table-column label="商品封面图" align="center" prop="imgurl">
          <template slot-scope="scope">
            <el-image
              fit="cover"
              :src="scope.row.imgurl"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="handleDeletegl(scope.row)"
              v-hasPermi="['system:fl:remove']"
              >删除关联</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  getFlList,
  addFlList,
  delFlList,
  updateFlList,
  getFlRelation,
  addFlRelation,
  delFlRelation,
} from "@/api/yfs/box_class.js";
import importForm from "@/components/importForm/put_box.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect, importForm },
  name: "Fl",
  data() {
    return {
      dialogTableVisible_2: false,
      dialogVisible_1: false,
      //关联列表
      gridData: [],
      title: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盒子分类表格数据
      flList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        time: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "盒子分类名称不能为空", trigger: "blur" },
        ],
        time: [{ required: true, message: "时间不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getFlList();
  },
  methods: {
    update(e) {
      console.log(e);
      // this.form.fid = row.id;
      // this.form.topId
      let data = {
        id: this.form.fid,
        topId: this.form.topId,
        goodsId: e.id,
      };
      addFlRelation(data).then((res) => {
        console.log(res);
        this.dialogVisible_1 = false;
        this.$modal.msgSuccess("关联成功");
      });
    },

    /** 查询盒子分类列表 */
    getFlList() {
      this.loading = true;
      getFlList(this.queryParams).then((response) => {
        this.flList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        topId: null,
        createDate: null,
      };
      // this.resetForm("form");
    },
    //添加盒子分类
    handleAdd(row) {
      this.title = "添加盒子分类--";
      this.open = true;
      this.reset();
      this.title = row.title;
      this.form.topId = row.id;
      console.log(this.title);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;

      this.form = row;
      this.open = true;
      this.title = "修改盒子分类";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFlList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getFlList();
            });
          } else {
            addFlList(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getFlList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除盒子分类编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFlList(ids);
        })
        .then(() => {
          this.getFlList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    //查看关联
    handlelookRelation(row) {
      let data = {
        fid: row.id,
        topId: row.topId,
      };
      this.look = data;

      getFlRelation(data).then((res) => {
        console.log(res);
        this.dialogTableVisible_2 = true;
        this.gridData = res.data;
      });
    },

    //产生关联
    handleAddRelation(row) {
      this.dialogVisible_1 = true;
      this.form.fid = row.id;
      this.form.topId = row.topId;
    },
    //删除关联
    handleDeletegl(row) {
      let data = {
        id: this.look.fid,
        topId: this.look.topId,
        goodsId: row.id,
      };
      this.$modal
        .confirm('是否确认解除"' + row.title + "的绑定？")
        .then(function () {
          return delFlRelation(data);
        })
        .then(() => {
          // this.getFlList();
          this.$modal.msgSuccess("删除成功");

          getFlRelation({
            fid: this.look.fid,
            topId: this.look.topId,
          }).then((res) => {
            console.log(res);
            // this.dialogTableVisible_2 = true;
            this.gridData = res.data;
          });
          // this.dialogTableVisible_2 = false;
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/fl/export",
        {
          ...this.queryParams,
        },
        `fl_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
